﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TorchView.GameContent.Data;
using System.ComponentModel;

namespace TorchView.GameContent.Skills
{
  public sealed class ExecuteSkill : IDataSerializable
  {
    public const string BLOCK_NAME = "EXECUTE_SKILL";

    public ExecuteSkill()
    {
      Skills = new List<string>(1);
    }

    #region IDataSerializable Members

    public DataBlock MakeDataBlock()
    {
      var block = Data == null ? new DataBlock(BLOCK_NAME) : Data;

      block.RemoveAllProperties(p => p.Name == "SKILL");
      foreach (var skill in Skills)
        block.Properties.Add(new DataProperty { Name = "SKILL", Value = skill, Type = PropertyType.STRING });

      return block;
    }

    public void LoadFromDataBlock(DataBlock block)
    {
      if (block == null)
        throw new ArgumentNullException("block");
      if (block.Name != BLOCK_NAME)
        throw new ArgumentException("Incorrect block type, only " + BLOCK_NAME + " is supported.");

      Data = block;

      Skills.Clear();
      Skills.AddRange(block.GetProperties("SKILL").Select(p => p.Value as string).Where(s => s != null));
    }

    #endregion
    
    public DataBlock Data { get; set; }

    [DisplayName("Skills"), Category("Skills"), Description("Skill List")]
    public List<string> Skills { get; private set; }

    public override string ToString()
    {
      return BLOCK_NAME;
    }
  }
}
